package com.hurui.xiaoming.activity.forgetpass;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.activity.login.LoginActivity;
import com.hurui.xiaoming.bean.Li_Regist_Result;
import com.hurui.xiaoming.bean.Li_Sms_Result;
import com.hurui.xiaoming.utils.DefultData;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgetPassActivity extends BaseActivity {

    EditText userPhone;
    EditText userPass;
    EditText secondPass;
    EditText identif;

    Button getSms;
    Button goto_main_btn;

    TextView goLogin;

    Map mapSms;
    Map mapRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass);

        mapSms = new HashMap();
        mapRegist = new HashMap();

        initView();
        initAction();
    }

    Li_Sms_Result li_sms_result;
    @Override
    public void onHttpSuccess(Object object) {
        super.onHttpSuccess(object);
        onToast("成功" + object.toString());
        Log.e("成功",object.toString());
        li_sms_result = (Li_Sms_Result)object;
    }

    @Override
    public void onHttpFail(int code, String msg) {
        super.onHttpFail(code, msg);
        onToast("失败" + code + "----" + msg);
    }

    @Override
    public void initView() {
        userPhone = (EditText) findViewById(R.id.userPhone);
        userPass = (EditText) findViewById(R.id.userPass);
        secondPass = (EditText) findViewById(R.id.secondPass);
        identif = (EditText) findViewById(R.id.identif);

        getSms = (Button) findViewById(R.id.getSms);
        goto_main_btn = (Button) findViewById(R.id.goto_main_btn);

        goLogin = (TextView) findViewById(R.id.goLogin);
    }

    @Override
    public void initAction() {
        getSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断电话号码是否为空,false为空
                if(checkData(userPhone)){
                    //获取短信验证码
                    mapSms.put("ui_phone",userPhone.getText().toString().trim());
                    mapSms.put("ui_id","");
                    getIntentSmsData(DefultData.getCode,mapSms);
                }else{
                    onToast("电话号码不能为空");
                }

            }
        });

        goto_main_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断电话号码是否为空,false为空
                if(checkData(userPhone)){
                    //检测账号密码是否为空
                    if(checkData(userPass) && checkData(secondPass)){
                        mapRegist.put("ui_phone",userPhone.getText().toString().trim());
                        mapRegist.put("ui_pwd",userPass.getText().toString().trim());
                        mapRegist.put("again_ui_pwd",secondPass.getText().toString().trim());
                        mapRegist.put("cd_code",identif.getText().toString().trim());
                        getIntentForgetPass(DefultData.forgetPass,mapRegist);
                    }else{
                        onToast("密码不能为空");
                    }
                }else{
                    onToast("电话号码不能为空");
                }
            }
        });


        goLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onOpen(new Intent(mContext , LoginActivity.class));
                onFinish();
            }
        });
    }

    //网络请求
    public void getIntentSmsData(String url, Map<String , String> parmas){
        Call<Li_Sms_Result> call = apiSevice.onHttpPostGetSms(url,parmas);
        call.enqueue(new Callback<Li_Sms_Result>() {
            @Override
            public void onResponse(Call<Li_Sms_Result> call, Response<Li_Sms_Result> response) {
                if(response.isSuccessful()){
                    Li_Sms_Result object = response.body() ;
                    if("success".equals(object.getStatus())){
                        getSms.setText("已发送");
                    }else{
                        onHttpFail(response.code() , "验证码发送失败");
                    }
                }else{
                    onHttpFail(response.code() , "response不成功");
                }
            }

            @Override
            public void onFailure(Call<Li_Sms_Result> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
            }
        });
    }

    //网络请求
    public void getIntentForgetPass(String url, Map<String , String> parmas){
        Call<Li_Regist_Result> call = apiSevice.onHttpPostForgrtPass(url,parmas);
        call.enqueue(new Callback<Li_Regist_Result>() {
            @Override
            public void onResponse(Call<Li_Regist_Result> call, Response<Li_Regist_Result> response) {
                if(response.isSuccessful()){
                    Li_Regist_Result object = response.body() ;
                    if("success".equals(object.getStatus())){
                        //todo 验证码发送成功
                        onOpen(new Intent(mContext , LoginActivity.class));
                        onFinish();
                    }else if("nouser".equals(object.getStatus())){
                        onToast("手机号不存在");
                    }else if("code_false".equals(object.getStatus())){
                        onToast("验证码错误");
                    }else if("password is wrong".equals(object.getStatus())){
                        onToast("两次密码不一致");
                    }else if("overdue".equals(object.getStatus())){
                        onToast("验证码过期");
                    }else{
                        onHttpFail(response.code() , "未知错误");
                    }
                }else{
                    onHttpFail(response.code() , "response不成功");
                }
            }

            @Override
            public void onFailure(Call<Li_Regist_Result> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
            }
        });
    }
}
